#!/bin/bash

# 集中式日志管理系统演示脚本

echo "=========================================="
echo "集中式日志管理系统演示 (动态库版本)"
echo "=========================================="

# 清理之前的运行
echo "清理之前的运行..."
pkill -f clog_server 2>/dev/null
pkill -f clog_viewer 2>/dev/null
rm -f /tmp/log_server.sock
sleep 1

# 启动日志服务器
echo "启动日志服务器..."
./bin/clog_server &
SERVER_PID=$!
sleep 2

echo "服务器启动完成 (PID: $SERVER_PID)"
echo ""

# 运行测试程序
echo "运行测试程序1..."
LD_LIBRARY_PATH=./lib ./bin/test_app1 &
TEST1_PID=$!

echo "运行测试程序2..."
LD_LIBRARY_PATH=./lib ./bin/test_app2 &
TEST2_PID=$!

# 等待测试程序完成
wait $TEST1_PID
wait $TEST2_PID

echo ""
echo "测试程序运行完成"
echo ""

# 显示日志文件
echo "生成的日志文件："
ls -la logs/
echo ""

# 演示日志查看功能
echo "=========================================="
echo "演示日志查看功能"
echo "=========================================="

echo "1. 查看test_app1的所有日志（前10行）："
echo "------------------------------------------"
head -10 logs/test_app1.log
echo ""

echo "2. 查看test_app2的错误日志："
echo "------------------------------------------"
grep "\[ERROR\]" logs/test_app2.log
echo ""

echo "3. 查看所有警告日志："
echo "------------------------------------------"
grep "\[WARNING\]" logs/*.log
echo ""

echo "4. 统计各种级别的日志数量："
echo "------------------------------------------"
echo "INFO 日志数量: $(grep -c "\[INFO\]" logs/*.log)"
echo "DEBUG 日志数量: $(grep -c "\[DEBUG\]" logs/*.log)"
echo "WARNING 日志数量: $(grep -c "\[WARNING\]" logs/*.log)"
echo "ERROR 日志数量: $(grep -c "\[ERROR\]" logs/*.log)"
echo ""

# 测试日志轮转功能
echo "5. 测试日志轮转功能："
echo "------------------------------------------"
echo "当前test_app1.log文件大小: $(wc -c < logs/test_app1.log) 字节"
echo "当前test_app2.log文件大小: $(wc -c < logs/test_app2.log) 字节"
echo ""

# 关闭服务器
echo "关闭日志服务器..."
kill $SERVER_PID 2>/dev/null
sleep 1

echo "演示完成！"
echo ""
echo "=========================================="
echo "使用说明"
echo "=========================================="
echo "1. 启动服务器: ./bin/clog_server"
echo "2. 在应用中使用动态库: 包含clog.h，链接libclog.so"
echo "3. 使用日志接口: LOGI(), LOGD(), LOGW(), LOGE()"
echo "4. 查看日志: ./bin/clog_viewer [选项]"
echo "   -p <进程名>  按进程过滤"
echo "   -l <级别>    按级别过滤 (info|debug|warning|error)"
echo "5. 配置文件: 编辑log_config.conf"
echo ""
echo "动态库文件: lib/libclog.so"
echo "静态库文件: lib/libclog.a"
echo "头文件: src/common/clog.h"
echo ""
